package com.example.controller;

import com.example.common.Result;
import com.example.entity.Learningrange;
import com.example.entity.PostLike;
import com.example.entity.Questions;
import com.example.service.QuestionsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问题前端接口
 */
@RestController
@RequestMapping("/questions")
public class QuestionsController {

    @Resource
    private QuestionsService questionsService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Questions questions) {
        questionsService.add(questions);
        return Result.success();
    }



    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        questionsService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        questionsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Questions questions) {
        questionsService.updateById(questions);
        return Result.success();
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Questions questions = questionsService.selectById(id);
        return Result.success(questions);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Questions questions ) {
        List<Questions> questionsList = questionsService.selectAll(questions);
        return Result.success(questionsList);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Questions questions,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Questions> pageInfo = questionsService.selectPage(questions, pageNum, pageSize);
        return Result.success(pageInfo);
    }

}
